//
// Created by 12992 on 24-10-13.
//

// You may need to build the project (run Qt uic code generator) to get "ui_DropUpload.h" resolved
#include <QPalette>
#include <QPainter>
#include <QDragEnterEvent>
#include <QMimeData>
#include "dropupload.h"
#include "ui_DropUpload.h"

namespace gui::widgets
{
    DropUpload::DropUpload(QWidget* parent) :
            QWidget(parent), ui(new Ui::DropUpload)
    {
        ui->setupUi(this);

        // 设置接受拖拽
        this->setAcceptDrops(true);
    }

    DropUpload::~DropUpload()
    {
        delete ui;
    }

    void DropUpload::paintEvent(QPaintEvent* event)
    {
        QWidget::paintEvent(event);
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        // 创建一个QPen对象，设置为虚线
        QPen pen;
        pen.setStyle(Qt::DashLine); // 设置虚线样式
        pen.setWidth(10); // 设置线宽
        pen.setColor(QColor(255, 255, 255, 128)); // 设置颜色
        painter.setPen(pen); // 将画笔设置给画家

        // 绘制矩形轮廓
        QRect rectangle(10, 10, this->width() - 20, this->height() - 20);
        int radius = 15; // 圆角的半径
        painter.drawRoundedRect(rectangle, radius, radius);
    }

    void DropUpload::dragEnterEvent(QDragEnterEvent* event)
    {
        // 如果拖拽的数据包含文件，则接受事件
        if (event->mimeData()->hasUrls())
        {
            event->acceptProposedAction();
        }
    }

    void DropUpload::dropEvent(QDropEvent* event)
    {
        // 获取所有拖拽的文件URL
        const QMimeData* mimeData = event->mimeData();
        if (mimeData->hasUrls())
        {
            QList<QUrl> urlList = mimeData->urls();
            // 对于每个URL，转换为本地文件路径并处理
            for (const QUrl& url: urlList)
            {
                QString filePath = url.toLocalFile();
                // 在这里处理文件，例如上传
                // handleFileUpload(filePath);
                qDebug() << "Dropped file:" << filePath;
                this->fileUploaded(filePath);
            }
        }
        event->acceptProposedAction();
    }
} // gui::widgets
